package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.product.ProductTypeListResultVO;
import com.sansan.qiangji.VO.base.product.ProductTypeVO;
import com.sansan.qiangji.VO.early.EarlyWarningListResultVO;
import com.sansan.qiangji.VO.early.EarlyWarningVO;
import com.sansan.qiangji.entity.base.Product;
import com.sansan.qiangji.entity.base.ProductType;
import com.sansan.qiangji.entity.base.example.ProductTypeExample;
import com.sansan.qiangji.entity.early.EarlyWarning;
import com.sansan.qiangji.entity.early.example.EarlyWarningExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.ProductTypeAddForm;
import com.sansan.qiangji.form.base.ProductTypeEditForm;
import com.sansan.qiangji.form.base.ProductTypeListForm;
import com.sansan.qiangji.form.early.EarlyWarningListForm;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.service.inter.base.ProductTypeService;
import com.sansan.qiangji.service.inter.early.EarlyWarningService;
import com.sansan.qiangji.utils.ConvertUtil;
import com.sansan.qiangji.utils.ResultVOUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhaobc
 * @description
 * @date 2020-11-18
 */
@Service
@Slf4j
public class ProductTypeReal {

    @Autowired
    private ProductTypeService productTypeService;

    public ProductTypeListResultVO getproductTypeList(ProductTypeListForm listForm){
        ProductTypeExample productTypeExample = new ProductTypeExample();
        int pageNum = listForm.getPageNum();
        int pageSize = listForm.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        ProductTypeExample.Criteria criteria = productTypeExample.createCriteria();
        if(!"".equals(listForm.getTypeName()) && listForm.getTypeName() != null)
            criteria.andTypeNameLike("%" + listForm.getTypeName() + "%");
        List<ProductType> productTypes =  productTypeService.selectByExample(productTypeExample);
        PageResult<ProductType> ProductTypePageResult = PageUtil.getPageResult(new PageInfo<>(productTypes));
        ProductTypeListResultVO productTypeListResultVO = new ProductTypeListResultVO();
        BeanUtils.copyProperties(ProductTypePageResult, productTypeListResultVO);
        List<ProductTypeVO> productTypeVOS = new ArrayList<ProductTypeVO>();
        for (ProductType productType : ProductTypePageResult.getContent()){
            ProductTypeVO productTypeVO = new ProductTypeVO();
            BeanUtils.copyProperties(productType, productTypeVO);
            productTypeVOS.add(productTypeVO);
        }
        productTypeListResultVO.setProductTypeVOS(productTypeVOS);
        return productTypeListResultVO;
    }
    public ResultVO<Object> editProductType(ProductTypeEditForm productTypeEditForm){
        ProductType productType = new ProductType();
        ProductTypeExample productTypeExample = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = productTypeExample.createCriteria();
        criteria.andTypeNameEqualTo(productTypeEditForm.getTypeName());
        criteria.andIdNotEqualTo(productTypeEditForm.getId());
        //查询是否已存在物品类型
        List<ProductType> productTypes =  productTypeService.selectByExample(productTypeExample);
        if(productTypes.size()>0){
            return ResultVOUtil.error("400","该物品类型已存在!");
        }
        BeanUtils.copyProperties(productTypeEditForm,productType);
        productType.setCreateTime(new Date());
        int row = productTypeService.updateByPrimaryKey(productType);
        if(row==0){
            log.error("【物品类型】编辑修改失败");
            return ResultVOUtil.error("400","编辑修改失败!");
        }
        return  ResultVOUtil.success( null);
    }
    public ResultVO<Object> addProductType(ProductTypeAddForm productTypeAddForm){
        ProductType productType = new ProductType();
        ProductTypeExample productTypeExample = new ProductTypeExample();
        ProductTypeExample.Criteria criteria = productTypeExample.createCriteria();
        criteria.andTypeNameEqualTo(productTypeAddForm.getTypeName());
        //查询是否已存在物品类型
        List<ProductType> productTypes =  productTypeService.selectByExample(productTypeExample);
        if(productTypes.size()>0){
            return ResultVOUtil.error("400","该物品类型已存在!");
        }
        BeanUtils.copyProperties(productTypeAddForm,productType);
        productType.setCreateTime(new Date());
        int row = productTypeService.insertSelective(productType);
        if(row==0){
            log.error("【物品类型】添加失败");
            return ResultVOUtil.error("400","添加失败!");
        }
        return  ResultVOUtil.success( null);
    }
    @Transactional
    public ResultVO<Object> deleteProductType(Integer id){
        int row = productTypeService.deleteByPrimaryKey(id);
        if(row==0){
            log.error("【物品类型】删除失败");
            return ResultVOUtil.error("400","删除失败!");
        }
        return  ResultVOUtil.success( null);
    }

}
